//
//  ViewController.m
//  NativeAppWithFlutter
//
//  Created by 门超 on 2021/3/20.
//

#import "ViewController.h"
#import "AppDelegate.h"
#import <Flutter/Flutter.h>
#import "FirstViewController.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 100, 160, 40);
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button setTitle:@"这是原生页面" forState:UIControlStateNormal];
    [self.view addSubview:button];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(10, 180, 260, 40);
    [button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button2 setTitle:@"push-Flutter页面|有传参" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(gotoFlutter) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
                               
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(10, 260, 260, 40);
    [button3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button3 setTitle:@"present-指定Flutter页面" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(gotoFlutter3) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    

}


- (void) gotoFlutter{
    FlutterDartProject * dartProject = [[FlutterDartProject alloc] init];
    FlutterViewController *flutterVC =   [[FlutterViewController alloc]initWithProject:dartProject initialRoute:@"myApp" nibName:nil bundle:nil];
    __weak __typeof(self) weakSelf = self;
    NSString *channelName = @"flutter_call_native";
    
    //FlutterMethodChannel是flutter页面主动交互iOS页面
    FlutterMethodChannel *methodChannel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:flutterVC];
   
    //flutter页面方法触发
    [methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        // call.method 获取 flutter 给回到的方法名，要匹配到 channelName 对应的多个 发送方法名，一般需要判断区分
        // call.arguments 获取到 flutter 给到的参数，（比如跳转到另一个页面所需要参数）
        // result 是给flutter的回调， 该回调只能使用一次
        NSLog(@"---->call.method= %@ , call.arguments= %@ ", call.method, call.arguments);
        
        // flutter点击事件执行后需要iOS做的事
        if ([call.method isEqualToString:@"goto-page"]) {
            NSLog(@"ios push start...");
            FirstViewController *firstVC = [[FirstViewController alloc] init];
            [weakSelf.navigationController pushViewController:firstVC animated:YES];
            NSLog(@"ios push end...");
        }
        
        // flutter给iOS传的参数
        if ([call.method isEqualToString:@"flutter-params"]) {
            NSDictionary *dic = call.arguments;
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"flutter页面传过来的Dict"
                                                                                     message:[NSString stringWithFormat:@"%@",dic]
                                                                              preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"OK Action");
            }];
            
            [alertController addAction:okAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        
        //iOS给flutter返回值  
        if ([call.method isEqualToString:@"native-call-flutter"]) {
            NSLog(@"iOS call Flutter");
            if (result) {
                result(@"iOS返回给flutter的内容：Hello Flutter from native");
            }
        }
        if ([call.method isEqualToString:@"changeNavStatus"]) {
            NSString *arguments = call.arguments;
            if ([arguments containsString:@"show"]) {
                weakSelf.navigationController.navigationBarHidden = NO;
            }else{
                weakSelf.navigationController.navigationBarHidden = YES;
            }
        }
        if ([call.method isEqualToString:@"backToViewController"]) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
    //原生iOS跳转到Flutter页面
    [self.navigationController pushViewController:flutterVC animated:NO];
}

- (void)gotoFlutter3{
    
    FlutterDartProject * dartProject = [[FlutterDartProject alloc] init];
    FlutterViewController *flutterVC =   [[FlutterViewController alloc]initWithProject:dartProject initialRoute:@"home" nibName:nil bundle:nil];
    __weak __typeof(self) weakSelf = self;
    NSString *channelName = @"navitve-get-flutter";
    
    //FlutterMethodChannel是flutter页面主动交互iOS页面
    FlutterMethodChannel *methodChannel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:flutterVC];
    //flutter页面方法触发
    [methodChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        NSLog(@"---->call.method= %@ , call.arguments= %@ ", call.method, call.arguments);
        if ([call.method isEqualToString:@"backToViewController"]) {
            NSArray *viewcontrollers=self.navigationController.viewControllers;
            if(viewcontrollers.count>1) {
                if([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            }else{
                [self dismissViewControllerAnimated:YES completion:nil];
                
            }
        }
    }];
    
    flutterVC.title=@"flutterTitle";
    flutterVC.modalPresentationStyle = 0;
    [self presentViewController:flutterVC animated:YES completion:^{

    }];
}

@end
